<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>新增序列号规则</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item
        label="序列号名称"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="sequenceName"
      >
        <a-input placeholder="请填写序列号名称" v-model="form.sequenceName" />
      </a-form-model-item>
      <a-form-model-item
        label="序列号编码"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="sequenceCode"
      >
        <a-input placeholder="请填写序列号编码" v-model="form.sequenceCode" />
      </a-form-model-item>
      <a-form-model-item
        label="前缀"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="prefix"
      >
        <a-input placeholder="请填写前缀" v-model="form.prefix" />
      </a-form-model-item>
      <a-form-model-item
        label="后缀"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="suffix"
      >
        <a-input placeholder="请填写后缀" v-model="form.suffix" />
      </a-form-model-item>
      <a-form-model-item
        label="策略"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="strategy"
      >
        <a-select placeholder="请选择策略" style="width: 100%" allow-clear v-model="form.strategy">
          <a-select-option v-for="(d, index) in strategyOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item
        label="日期格式"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="dateFormat"
      >
        <a-select placeholder="请选择日期格式" style="width: 100%" allow-clear v-model="form.dateFormat">
          <a-select-option v-for="(d, index) in dateFormatOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item
        label="最小值"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="minValue"
      >
        <a-input placeholder="请填写最小值" v-model="form.minValue" />
      </a-form-model-item>
      <a-form-model-item
        label="最大值"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="maxValue"
      >
        <a-input placeholder="请填写最大值" v-model="form.maxValue" />
      </a-form-model-item>
      <a-form-model-item
        label="当前值"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="currentValue"
      >
        <a-input placeholder="请填写当前值" v-model="form.currentValue" />
      </a-form-model-item>
      <a-form-model-item
        label="每次生成多少个"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="generateNum"
      >
        <a-input placeholder="请填写每次生成多少个" v-model="form.generateNum" />
      </a-form-model-item>
      <a-form-model-item
        label="是否重置"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="reset"
      >
        <a-radio-group v-model="form.reset" button-style="solid">
          <a-radio-button value="Y">是</a-radio-button>
          <a-radio-button value="N">否</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item
        label="补值"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="repairValue"
      >
        <a-input placeholder="请填写补值" v-model="form.repairValue" />
      </a-form-model-item>
      <a-form-model-item
        label="阈值"
        has-feedback
        :labelCol="{span: 5}"
        :wrapperCol="{span: 15}"
        prop="threshold"
      >
        <a-input placeholder="请填写阈值" v-model="form.threshold" />
      </a-form-model-item>
    </a-form-model>
    <div class="bottom-control">
      <a-space>
        <a-button type="primary" @click="submitForm">
          保存
        </a-button>
        <a-button type="dashed" @click="cancel">
          取消
        </a-button>
      </a-space>
    </div>
  </a-drawer>
</template>
<script>
import { getSequence, saveSequence, updateSequence } from '@/api/system/sequence'

export default {
  components: {},
  props: {},
  data () {
    return {
      // 策略
      strategyOptions: [],
      // 日期格式
      dateFormatOptions: [],
      open: false,
      form: {
        sequenceName: undefined,
        sequenceCode: undefined,
        prefix: undefined,
        suffix: undefined,
        strategy: undefined,
        dateFormat: undefined,
        minValue: undefined,
        maxValue: undefined,
        currentValue: undefined,
        generateNum: undefined,
        reset: undefined,
        repairValue: undefined,
        threshold: undefined
      },
      rules: {
        sequenceName: [{ required: true, message: '序列号名称不能为空', trigger: 'blur' }],
        sequenceCode: [{ required: true, message: '序列号编码不能为空', trigger: 'blur' }]
      }
    }
  },
  watch: {},
  computed: {},
  methods: {
    rest () {
      this.form = {
        sequenceName: undefined,
        sequenceCode: undefined,
        prefix: undefined,
        suffix: undefined,
        strategy: undefined,
        dateFormat: undefined,
        minValue: undefined,
        maxValue: undefined,
        currentValue: undefined,
        generateNum: undefined,
        reset: 'Y',
        repairValue: undefined,
        threshold: undefined
      }
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    showForm (id) {
      this.open = true
      this.rest()
      if (id) {
        getSequence(id).then(res => {
          if (res.code === 200) {
            this.form = res.data
          }
        })
      }
    },
    onClose () {
      this.open = false
    },
    submitForm () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.ruleId) {
            updateSequence(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success(
                  '修改成功',
                  3
                )
                this.open = false
                this.$emit('ok')
              }
            })
          } else {
            saveSequence(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success(
                  '添加成功',
                  3
                )
                this.open = false
                this.$emit('ok')
              }
            })
          }
        }
      })
    }
  },
  created () {
    this.getDicts('sys_sequence_strategy').then(response => {
      this.strategyOptions = response.data
    })
    this.getDicts('date_format').then(response => {
      this.dateFormatOptions = response.data
    })
  },
  mounted () {
  }
}
</script>,
